package com.sgcc.sx.pmos.e_sx_mall.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.sx.pmos.arouter.mall.MallArouterPath;
import com.sgcc.sx.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.sx.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.sx.pmos.common.bean.StoreInfoBean;
import com.sgcc.sx.pmos.common.bean.StoreScreenBackBean;
import com.sgcc.sx.pmos.common.dialog.OnSearchClickListener;
import com.sgcc.sx.pmos.common.dialog.StoreScreenDialog;
import com.sgcc.sx.pmos.common.utils.DialogUtil;
import com.sgcc.sx.pmos.common.utils.ListPageUtil;
import com.sgcc.sx.pmos.common.utils.UserManager;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_mall.adapter.StoreAdapter;
import com.sgcc.sx.pmos.e_sx_mall.bean.StoreBean;
import com.sgcc.sx.pmos.e_sx_mall.contract.StoreContract;
import com.sgcc.sx.pmos.e_sx_mall.presenter.StorePresenter;

/**
 * 店铺
 */
public class StoreFragment extends MvpBaseRecycleFragment<StoreInfoBean, StorePresenter> implements StoreContract.View {

    private StoreAdapter mStoreAdapter;
    private RelativeLayout mRlStoreLeft;
    private RelativeLayout mRlStoreRight;
    private RelativeLayout rl_store_center;
    private LinearLayout mLlStoreScreening;
    private StoreScreenDialog storeScreenDialog;

    TextView rl_txt_left, rl_txt_right, rl_txt_center;
    ImageView xsdl_up_img, xsdl_down_img, xsdlmax_up_img, xsdlmax_doown_img, yhsl_up_img, yhsl_down_img;
    String saleEnergyYearOrder;//年以销售电量升序1 降序0
    String saleMemberNumOrder;//在售商品数量 升序1 降序0
    String userNumSort;//用户数量升序1 降序0
    int SortState = 0;
    int SortPosition = 0;
    StoreScreenBackBean storeScreenBackBean;

    @Override
    protected StorePresenter createPresenter() {
        return new StorePresenter();
    }


    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_store_layout, parent, false);
        findViewByIds(head_view);
        return head_view;
    }

    private void findViewByIds(View head_view) {
        mRlStoreLeft = head_view.findViewById(R.id.rl_store_left);
        mRlStoreRight = head_view.findViewById(R.id.rl_store_right);
        rl_store_center = head_view.findViewById(R.id.rl_store_center);

        mLlStoreScreening = head_view.findViewById(R.id.ll_store_screening);

        rl_txt_left = head_view.findViewById(R.id.rl_txt_left);
        rl_txt_right = head_view.findViewById(R.id.rl_txt_right);
        rl_txt_center = head_view.findViewById(R.id.rl_txt_center);

        xsdl_up_img = head_view.findViewById(R.id.xsdl_up_img);
        xsdl_down_img = head_view.findViewById(R.id.xsdl_down_img);
        xsdlmax_up_img = head_view.findViewById(R.id.xsdlmax_up_img);
        xsdlmax_doown_img = head_view.findViewById(R.id.xsdlmax_doown_img);
        yhsl_up_img = head_view.findViewById(R.id.yhsl_up_img);
        yhsl_down_img = head_view.findViewById(R.id.yhsl_down_img);

        mRlStoreLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setLeftSortUI();
                refresh(true);
            }
        });
        mRlStoreRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setRightSortUI();
                refresh(true);
            }
        });
        rl_store_center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setCenterSortUI();
                refresh(true);
            }
        });


        mLlStoreScreening.setOnClickListener(view ->
                DialogUtil.showStoreScreenDialog(getContext(), new OnSearchClickListener<StoreScreenBackBean>() {
                    @Override
                    public void onClick(StoreScreenBackBean storeScreenBackBean) {
                        StoreFragment.this.storeScreenBackBean = storeScreenBackBean;
                        refresh(true);
                    }
                }));
    }

    @Override
    public boolean setFloatingButtonVisibility() {
        return true;
    }

    @Override
    public int setFloatingButtonImage() {
        return R.mipmap.store_focus;
    }

    @Override
    protected void initViews() {
        super.initViews();
        floating_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(PersonalCenterArouterPath.PERSONALCENTER_FOCUS)
                            .navigation();
                }
            }
        });
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mStoreAdapter = new StoreAdapter(mContext);
        mStoreAdapter.setOnItemClickListener(new StoreAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position, StoreInfoBean bean) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_STOREHOMEPAGE)
                        .withString("membersSdId", bean.getMembersSdId())
                        .navigation();
            }
        });
        return mStoreAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mListSize = listSize;
        mPresenter.getStoreList(listSize, isloading, saleMemberNumOrder, saleEnergyYearOrder, storeScreenBackBean,userNumSort);
    }

    int mListSize;

    @Override
    public void querySuccess(StoreBean response) {
        if (response != null) {
            if (response.getCode() == 200) {
                if (response.getRows() != null) {
                    loadSuccess(ListPageUtil.isHasNextPage(response.getTotal(), mListSize), response.getRows());
                }
            } else {
                loadFalied();
            }
        }
    }

    @Override
    public void queryFailed() {
        loadFalied();
    }

    private void initSortUI() {
        rl_txt_left.setTextColor(0xff999999);
        rl_txt_right.setTextColor(0xff999999);
        rl_txt_center.setTextColor(0xff999999);

        xsdl_up_img.setBackgroundResource(R.mipmap.mall_top_store_b_up);
        xsdl_down_img.setBackgroundResource(R.mipmap.mall_top_store_b);


        xsdlmax_up_img.setBackgroundResource(R.mipmap.mall_top_store_b_up);
        xsdlmax_doown_img.setBackgroundResource(R.mipmap.mall_top_store_b);

        yhsl_up_img.setBackgroundResource(R.mipmap.mall_top_store_b_up);
        yhsl_down_img.setBackgroundResource(R.mipmap.mall_top_store_b);
    }

    private void setCenterSortUI() {
        if (SortPosition != 0) {
            SortState = 1;
        } else {
            sortStatadd();
        }
        initSortUI();

        if (SortState == 1) {
            rl_txt_center.setTextColor(0xff3274F0);
            yhsl_up_img.setBackgroundResource(R.mipmap.mall_top_store_b_up_pre);
            saleMemberNumOrder = "";
            saleEnergyYearOrder = "";
            userNumSort = "1";

        } else if (SortState == 2) {
            rl_txt_center.setTextColor(0xff3274F0);
            yhsl_down_img.setBackgroundResource(R.mipmap.mall_top_store_b_pre);
            saleMemberNumOrder = "";
            saleEnergyYearOrder = "";
            userNumSort = "0";
        } else {
            saleMemberNumOrder = "";
            saleEnergyYearOrder = "";
            userNumSort = "";
        }
        SortPosition = 0;
    }

    private void setLeftSortUI() {
        if (SortPosition != 1) {
            SortState = 1;
        } else {
            sortStatadd();
        }
        initSortUI();

        if (SortState == 1) {
            rl_txt_left.setTextColor(0xff3274F0);
            xsdl_up_img.setBackgroundResource(R.mipmap.mall_top_store_b_up_pre);
            saleMemberNumOrder = "";
            saleEnergyYearOrder = "1";
            userNumSort = "";
        } else if (SortState == 2) {
            rl_txt_left.setTextColor(0xff3274F0);
            xsdl_down_img.setBackgroundResource(R.mipmap.mall_top_store_b_pre);
            saleMemberNumOrder = "";
            saleEnergyYearOrder = "0";
            userNumSort = "";
        } else {
            saleMemberNumOrder = "";
            saleEnergyYearOrder = "";
            userNumSort = "";
        }
        SortPosition = 1;
    }

    private void setRightSortUI() {
        if (SortPosition != 2) {
            SortState = 1;
        } else {
            sortStatadd();
        }

        initSortUI();

        if (SortState == 1) {
            rl_txt_right.setTextColor(0xff3274F0);
            xsdlmax_up_img.setBackgroundResource(R.mipmap.mall_top_store_b_up_pre);
            saleMemberNumOrder = "1";
            saleEnergyYearOrder = "";
            userNumSort = "";
        } else if (SortState == 2) {
            rl_txt_right.setTextColor(0xff3274F0);
            xsdlmax_doown_img.setBackgroundResource(R.mipmap.mall_top_store_b_pre);
            saleMemberNumOrder = "0";
            saleEnergyYearOrder = "";
            userNumSort = "";
        } else {
            saleMemberNumOrder = "";
            saleEnergyYearOrder = "";
            userNumSort = "";
        }
        SortPosition = 2;
    }

    private void sortStatadd() {
        if (SortState < 2) {
            SortState++;
        } else {
            SortState = 0;
        }
    }

}
